package com.wangsd.web.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangsd.web.coupon.entity.Coupon;
import com.wangsd.web.coupon.entity.MemberCoupon;
import com.wangsd.web.coupon.mapper.MemberCouponMapper;
import com.wangsd.web.coupon.service.MemberCouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-09-26
 */
@Service
public class MemberCouponServiceImpl extends ServiceImpl<MemberCouponMapper, MemberCoupon> implements MemberCouponService {

    @Resource
    MemberCouponMapper memberCouponMapper;

    @Override
    public Integer selectByProductIdAndOpenid(String productId, String openid) {
        QueryWrapper<MemberCoupon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("coupon_id",productId);
        queryWrapper.eq("openid",openid);
        return memberCouponMapper.selectCount(queryWrapper);
    }

    @Override
    public Long selectNewGiftByProductIdAndOpenid(List<String> productIds, String openid) {
        QueryWrapper<MemberCoupon> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("product_id",productIds);
        queryWrapper.eq("openid",openid);
        long count = memberCouponMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public IPage<MemberCoupon> selectList(String code, Integer pageNum, Integer pageSize, MemberCoupon coupon) {
        Page<MemberCoupon> page = new Page<>(pageNum, pageSize);
        if(coupon.getType()==1){
            IPage<MemberCoupon> couponIPage = memberCouponMapper.selectListXml(page, code, coupon);
            return couponIPage;
        }else{
            IPage<MemberCoupon> couponIPage = memberCouponMapper.selectBusinessListXml(page, code, coupon);
            return couponIPage;
        }
    }

    @Override
    public Map selectListStatisticsXml(String code, Integer pageNum, Integer pageSize, MemberCoupon coupon) {
        if(coupon.getType()==1){
            Map map = memberCouponMapper.selectListStatisticsXml(code, coupon);
            return map;
        }else{
            Map map = memberCouponMapper.selectBusinessListStatisticsXml(code, coupon);
            return map;
        }
    }



    @Override
    public void verificationCoupon(MemberCoupon memberCoupon) {
        MemberCoupon old = memberCouponMapper.selectOne(new QueryWrapper<MemberCoupon>()
                .eq("wx_coupon_id", memberCoupon.getWxCouponId())
                .eq("state", 0));
        if (old != null) {
            memberCoupon.setId(old.getId());
            memberCouponMapper.updateById(memberCoupon);
        }
    }
}
